package com.tuosha.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tuosha.dao.JieQianJqCatDao;
import com.tuosha.dao.JieQiansDao;
import com.tuosha.model.JieQianJqCatEntity;
import com.tuosha.model.JieQiansEntity;
import com.tuosha.model.JqCatsEntity;
import com.tuosha.service.JieQianJqCatService;
import com.tuosha.service.JieQiansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("jieQiansServiceImpl")
public class JieQiansServiceImpl implements JieQiansService {
	@Autowired
	private JieQiansDao jieQiansDao;

	@Autowired
	private JieQianJqCatDao jieQianJqCatDao;

	@Autowired
	private JieQianJqCatService jieQianJqCatServiceImpl;

	@Override
	public JieQiansEntity queryObject(Integer id){
		return jieQiansDao.queryObject(id);
	}
	
	@Override
	public List<JieQiansEntity> queryList(Map<String, Object> map){
		return jieQiansDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return jieQiansDao.queryTotal(map);
	}
	
	@Override
	public void save(JieQiansEntity jieQians){
		Date date=new Date();
		jieQians.setCreatedAt(date);
		jieQians.setView(0);
		jieQiansDao.save(jieQians);
	}
	
	@Override
	public int update(JieQiansEntity jieQians){
		Date date=new Date();
		jieQians.setUpdatedAt(date);
		return jieQiansDao.update(jieQians);
	}
	
	@Override
	public int delete(Integer id){
		return jieQiansDao.delete(id);
	}
	
	@Override
	public int deleteBatch(Integer[] ids){
		return jieQiansDao.deleteBatch(ids);
	}

	@Override
	public int delById(Integer id) {
		Date date=new Date();
		JieQiansEntity jieQiansEntity= new JieQiansEntity();
		jieQiansEntity.setId(id);
		jieQiansEntity.setDeletedAt(date);
		//删除关联关系表信息
		List<JieQianJqCatEntity> jqcats = jieQianJqCatDao.queryAllList(id);
		if (jqcats.size()!=0) {
			for (JieQianJqCatEntity cat : jqcats) {
				jieQianJqCatServiceImpl.delById(cat.getId());
			}
		}
		return jieQiansDao.update(jieQiansEntity);
	}


	public int delPatch(String  tiestr){
		String[] strs = tiestr.split(",");
		if(strs!=null&&strs.length>0){
			for (String str : strs) {
				delById(Integer.parseInt(str));
			}
		}
		return 0;
	}

	public JieQiansEntity queryName(String name){
		return jieQiansDao.queryName(name);
	}


	@Override
	public void savelist(JSONObject jsonObject){
		//保存口子信息
		JieQiansEntity jieQians = new JieQiansEntity();
		Date date=new Date();
		jieQians.setName(jsonObject.getString("name"));
		jieQians.setAdvantage(jsonObject.getString("advantage"));
		jieQians.setImage(jsonObject.getString("image"));
		jieQians.setHot(jsonObject.getInteger("hot"));
		jieQians.setPass(jsonObject.getDouble("pass"));
		jieQians.setMin(jsonObject.getInteger("min"));
		jieQians.setMax(jsonObject.getInteger("max"));
		jieQians.setTimeUnit(jsonObject.getString("timeUnit"));
		jieQians.setMinTime(jsonObject.getInteger("minTime"));
		jieQians.setMaxTime(jsonObject.getInteger("maxTime"));
		jieQians.setRate(jsonObject.getDouble("rate"));
		jieQians.setFangkuanTime(jsonObject.getString("fangkuanTime"));
		jieQians.setLiucheng(jsonObject.getString("liucheng"));
		jieQians.setTiaojian(jsonObject.getString("tiaojian"));
		jieQians.setCailiao(jsonObject.getString("cailiao"));
		jieQians.setLink(jsonObject.getString("link"));
		jieQians.setView(0);
		jieQians.setCreatedAt(date);
		//insert --- return jieqians id
		jieQiansDao.savelist(jieQians);
		Integer	jqid =jieQians.getId();

				//把口子文章类型转换成JSON数组
		String jqcatArray = jsonObject.getString("jqarr");
		String[] jqarray = jqcatArray.split(",");

		//查询文章id是否有关系表记录
		List<JieQianJqCatEntity> jqcat = jieQianJqCatDao.queryAllList(jqid);
		JSONArray array= JSONArray.parseArray(JSON.toJSONString(jqcat));
		if (jqcat.size()!=0) {
			//遍历form的文章类型
			boolean flag;
			for (int i = 0; i < jqarray.length; i++) {

				flag = false;
				//遍历对应文章id的  关系表记录
				int j=0;
				for (JieQianJqCatEntity jqs : jqcat) {
					if (jqarray[i].equals(jqs.getJqCatId().toString())) {
						//存在对应类型的记录 跳到下一个
						flag = true;
						array.getJSONObject(j).put("have",true);
						break;
					}
					j++;
				}
				if(!flag) {
					//插入文章与类型的  关系记录
					JieQianJqCatEntity jj = new JieQianJqCatEntity();
					jj.setJieQianId(jqid);
					jj.setJqCatId(Integer.parseInt(jqarray[i]));
					Date date1 = new Date();
					jj.setCreatedAt(date1);
					jieQianJqCatDao.save(jj);
				}

			}
			for (int i = 0; i < array.size(); i++) {
				if (!array.getJSONObject(i).containsKey("have")){
					//删除多余的
					jieQianJqCatServiceImpl.delById(array.getJSONObject(i).getInteger("id"));
				}
			}
		}else{
			//遍历文章类型
			for (int i = 0; i < jqarray.length; i++) {
				//插入文章与类型的  关系记录
				JieQianJqCatEntity jj = new JieQianJqCatEntity();
				jj.setJieQianId(jqid);
				jj.setJqCatId(Integer.parseInt(jqarray[i]));
				Date date1 = new Date();
				jj.setCreatedAt(date1);
				jieQianJqCatDao.save(jj);
			}
		}
	}

    @Override
    public int updatelist(JSONObject jsonObject){
        JieQiansEntity jieQians = new JieQiansEntity();
        jieQians.setId(jsonObject.getInteger("id"));
        jieQians.setName(jsonObject.getString("name"));
        jieQians.setAdvantage(jsonObject.getString("advantage"));
        jieQians.setImage(jsonObject.getString("image"));
        jieQians.setHot(jsonObject.getInteger("hot"));
        jieQians.setPass(jsonObject.getDouble("pass"));
        jieQians.setMin(jsonObject.getInteger("min"));
        jieQians.setMax(jsonObject.getInteger("max"));
        jieQians.setTimeUnit(jsonObject.getString("timeUnit"));
        jieQians.setMinTime(jsonObject.getInteger("minTime"));
        jieQians.setMaxTime(jsonObject.getInteger("maxTime"));
        jieQians.setRate(jsonObject.getDouble("rate"));
        jieQians.setFangkuanTime(jsonObject.getString("fangkuanTime"));
        jieQians.setLiucheng(jsonObject.getString("liucheng"));
        jieQians.setTiaojian(jsonObject.getString("tiaojian"));
        jieQians.setCailiao(jsonObject.getString("cailiao"));
        jieQians.setLink(jsonObject.getString("link"));
        jieQians.setView(0);
        Date date=new Date();
        jieQians.setUpdatedAt(date);


        //把口子文章类型转换成JSON数组
        String jqcatArray = jsonObject.getString("jqarr");
        String[] jqarray = jqcatArray.split(",");

        //查询文章id是否有关系表记录
        List<JieQianJqCatEntity> jqcat = jieQianJqCatDao.queryAllList(jsonObject.getInteger("id"));

		JSONArray array= JSONArray.parseArray(JSON.toJSONString(jqcat));

        if (jqcat.size()!=0) {
            //遍历form传入的文章类型
            boolean flag;
            for (int i = 0; i < jqarray.length; i++) {

                flag = false;
                //遍历对应文章id的  关系表记录
				int j=0;
                for (JieQianJqCatEntity jqs : jqcat) {
					if (jqarray[i].equals(jqs.getJqCatId().toString())) {
                        //存在对应类型的记录 跳到下一个
						array.getJSONObject(j).put("have",true);
                        flag = true;
						break;
                    }

                    j++;
                }
                if(!flag) {
                    //插入文章与类型的  关系记录
                    JieQianJqCatEntity jj = new JieQianJqCatEntity();
                    jj.setJieQianId(jsonObject.getInteger("id"));
                    jj.setJqCatId(Integer.parseInt(jqarray[i]));
                    Date date1 = new Date();
                    jj.setCreatedAt(date1);
                    System.out.println(jj.toString());
                    jieQianJqCatDao.save(jj);
                }

            }
			for (int i = 0; i < array.size(); i++) {

            	if (!array.getJSONObject(i).containsKey("have")){
					//删除多余的

					jieQianJqCatServiceImpl.delById(array.getJSONObject(i).getInteger("id"));
				}
			}
        }else{
            //遍历文章类型
            for (int i = 0; i < jqarray.length; i++) {
                //插入文章与类型的  关系记录
                JieQianJqCatEntity jj = new JieQianJqCatEntity();
                jj.setJieQianId(jsonObject.getInteger("id"));
                jj.setJqCatId(Integer.parseInt(jqarray[i]));
                Date date1 = new Date();
                jj.setCreatedAt(date1);
                jieQianJqCatDao.save(jj);
            }
        }

        return jieQiansDao.update(jieQians);
    }
}
